package com.andaily.domain.dto.thing;

import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.dto.user.UserDto;
import com.andaily.domain.shared.DateUtils;
import com.andaily.domain.thing.*;
import com.andaily.web.context.BeanProvider;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Shengzhao Li
 * @see com.andaily.domain.thing.Thing
 */
public class ThingDto extends AbstractDTO {

    private static ThingRepository thingRepository() {
        return BeanProvider.getBean(ThingRepository.class);
    }

    //内容
    private String content;
    private ThingPriority priority = ThingPriority.defaultPriority();
    private String eventGuid;
    private String eventName;
    private String startDate;

    private ThingStatus status = ThingStatus.CREATED;
    private UserDto owner;
    private String createTime;
    private String cancelTime;
    private String endTime;

    private List<EventDto> availableEvents = new ArrayList<EventDto>();
    private boolean addNext;
    private boolean showWarning;

    public ThingDto() {
    }

    public ThingDto(ThingStatus status) {
        this.status = status;
    }

    public ThingDto(Thing thing) {
        this.guid = thing.guid();
        this.content = thing.content();
        this.status = thing.status();
        this.createTime = DateUtils.toDateText(thing.createTime(), DateUtils.DATE_TIME_FORMAT);

        this.priority = thing.priority();
        this.owner = new UserDto(thing.owner());
        final Date date = thing.startDate();
        if (date != null) {
            this.startDate = DateUtils.toDateText(date);
            this.showWarning = (date.before(DateUtils.now()) && ThingStatus.CREATED.equals(thing.status()));
        }
        final Event event = thing.event();
        if (event != null) {
            this.eventGuid = event.guid();
            this.eventName = event.name();
        }
        final Date cancelTime = thing.cancelTime();
        if (cancelTime != null) {
            this.cancelTime = DateUtils.toDateText(cancelTime, DateUtils.DATE_TIME_FORMAT);
        }
        final Date endTime = thing.endTime();
        if (endTime != null) {
            this.endTime = DateUtils.toDateText(endTime, DateUtils.DATE_TIME_FORMAT);
        }
    }

    public ThingPriority[] getPriorities() {
        return ThingPriority.values();
    }

    public String getPriorityCssStyle() {
        String style = "";
        switch (priority) {
            case HIGH:
            case HIGHER:
                style = "red";
                break;
            case LOW:
                style = "color-b";
                break;
            case LOWER:
                style = "gray";
                break;
        }
        return style;
    }

    public boolean isAllowAction() {
        return ThingStatus.CREATED.equals(status)
                || ThingStatus.PENDING.equals(status);
    }

    public boolean isShowWarning() {
        return showWarning;
    }

    public List<EventDto> getAvailableEvents() {
        return availableEvents;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public boolean isAddNext() {
        return addNext;
    }

    public void setAddNext(boolean addNext) {
        this.addNext = addNext;
    }

    public void setAvailableEvents(List<EventDto> availableEvents) {
        this.availableEvents = availableEvents;
    }

    public String getCancelTime() {
        return cancelTime;
    }

    public void setCancelTime(String cancelTime) {
        this.cancelTime = cancelTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public ThingPriority getPriority() {
        return priority;
    }

    public void setPriority(ThingPriority priority) {
        this.priority = priority;
    }

    public String getEventGuid() {
        return eventGuid;
    }

    public void setEventGuid(String eventGuid) {
        this.eventGuid = eventGuid;
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public ThingStatus getStatus() {
        return status;
    }

    public void setStatus(ThingStatus status) {
        this.status = status;
    }

    public UserDto getOwner() {
        return owner;
    }

    public void setOwner(UserDto owner) {
        this.owner = owner;
    }

    public Thing instance() {
        return isNewly() ? new Thing() : thingRepository().findByGuid(this.guid);
    }

    public static List<ThingDto> toDtos(List<Thing> things) {
        List<ThingDto> thingDtos = new ArrayList<ThingDto>(things.size());
        for (Thing thing : things) {
            thingDtos.add(new ThingDto(thing));
        }

        return thingDtos;
    }
}
